package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.User;
import com.itheima.service.MemberService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author fr
 * @date 2020-03-08 9:46
 */
@RestController
@RequestMapping("/member_f")
public class memberController_f {

    @Reference
    private MemberService memberService;

    //分页查询
    @RequestMapping("/findPage")
    public Result findMember(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult pageResult = memberService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

    //新增会员数据回显
    @RequestMapping("/findTeacher")
    public Result findHealthTeachers(){
        try {
            List<User> names = memberService.teacherName();
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS,names);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MEMBER_FAIL);
        }
    }
    @RequestMapping("/addMember")
    public Result addMember(@RequestBody Member member1){
        //校验手机号
        String telephone = member1.getPhoneNumber();
        Pattern regex_tel = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$");
        boolean b_tel = regex_tel.matcher(telephone).matches();
        if (!b_tel) {
            return new Result(false, "手机号输入有误");
        }
        try {
            //校验成功判断是否是会员
            Member member = memberService.findByTelephone(telephone);
            if (member == null) {
                memberService.add(member1);
                return new Result(true,MessageConstant.ADD_MEMBER_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.ADD_MEMBER_SUCCESS);
    }

    //编辑数据回显
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Member member = memberService.findById(id);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS,member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MEMBER_FAIL);
        }
    }
    @RequestMapping("/editMember")
    public Result editMember(@RequestBody Member member1){
        //校验手机号
        String telephone = member1.getPhoneNumber();
        Pattern regex_tel = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$");
        boolean b_tel = regex_tel.matcher(telephone).matches();
        if (!b_tel) {
            return new Result(false, "手机号输入有误");
        }
        try {
            //校验成功判断是否是会员
            Member member = memberService.findByTelephone(telephone);
            if (member == null) {
                memberService.editMember(member1);
            }
            return new Result(true,MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MEMBER_FAIL);
        }
    }

    //删除
    @RequestMapping("/delMember")
    public Result delMember(Integer id){
        try {
            memberService.delMember(id);
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MEMBER_FAIL);
        }
    }


}
